import React, { useImperativeHandle, forwardRef, useRef } from 'react'
import { Form, Input, Row, Col, DatePicker, Switch } from 'antd'
import SubjectSelector from './SubjectSelector'
import AddressInput from './AddressInput'
const { Item } = Form
const { TextArea } = Input
const shortLabel = { span: 6 }
const shortWrap = { span: 16 }

// 改变仲裁主体回调
function onChangeContractId (o, formRef, addressRef, pState, pDispatch) {
  const currentAddressList = pState.addressList.filter(li => li.contractid === o.id && li.status === 1).map((li, i) => {
    li.value = `${li.address}${i}`
    li.label = `(${i + 1})、${li.address || '无地址'}`
    return li
  })
  pDispatch({ type: 'currentAddressList', data: currentAddressList })
  if (currentAddressList.length === 1) {
    addressRef.current.changeAddress(formRef, currentAddressList[0], pState)
  }
}

// formRef, caseDetail, data, theme, addressList, contractList, setContractList, currentAddressList, setCurrentAddressList
function Main ({ formRef, pState, pDispatch, required = true, hasLast = true }, ref) {
  const subjectRef = useRef(null)
  const addressRef = useRef(null)
  useImperativeHandle(ref, () => ({
    changeSubject: (type, formRef, caseDetail, data, dispatch) => {
      subjectRef.current.changeSubject(type, formRef, caseDetail, data, dispatch)
    }
  }))
  return (
    <>
      <Item name='contract_type' noStyle><span /></Item>
      <Item name='id' noStyle><span /></Item>
      <SubjectSelector ref={subjectRef} formRef={formRef} pState={pState} pDispatch={pDispatch} onChangeContractId={o => onChangeContractId(o, formRef, addressRef, pState, pDispatch)} required={required} />
      <AddressInput ref={addressRef} addressList={pState.currentAddressList} pState={pState} formRef={formRef} required={required} />
      <Item name='mobile' label='移动电话'>
        <Input placeholder='请输入移动电话' />
      </Item>
      <Item name='tel' label='住宅电话'>
        <Input placeholder='请输入住宅电话' />
      </Item>
      <Item name='tel_office' label='办公电话'>
        <Input placeholder='请输入办公电话' />
      </Item>
      <Item name='contract' label='收件人'>
        <Input placeholder='请输入' />
      </Item>
      <Item name='structure' label='单位'>
        <Input placeholder='请输入单位' />
      </Item>
      <Row>
        <Col span={12}>
          <Item name='processerid' noStyle><span /></Item>
          <Item name='processer' label='办案秘书' labelCol={shortLabel} wrapperCol={shortWrap} required={[{ required: true, message: '请输入办案秘书' }]}>
            <Input disabled />
          </Item>
        </Col>
        <Col span={12}>
          <Item name='processer_tel' label='电话' labelCol={shortLabel} wrapperCol={shortWrap} required={[{ required: true, message: '请输入电话' }]}>
            <Input disabled />
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={12}>
          <Item name='send_datetime' label='收寄日期' labelCol={shortLabel} wrapperCol={shortWrap} rules={[{ type: 'object', required: true, message: '请选择收寄日期' }]}>
            <DatePicker placeholder='请选择收寄日期' style={{ width: '100%' }} />
          </Item>
        </Col>
      </Row>
      <Item name='content' label='送达内容' rules={[{ required: true, message: '请输入送达内容' }]}>
        <TextArea rows={3} placeholder='请输入送达内容' />
      </Item>
      <Item name='remark' label='备注'>
        <TextArea rows={3} placeholder='请输入备注' />
      </Item>
      {hasLast ? (
        <Item name='islast_known_place' label='是否为最后一个为人所知的地址' labelCol={{}} valuePropName='checked'>
          <Switch checkedChildren='是' unCheckedChildren='否' />
        </Item>
      ) : null}
    </>
  )
}

export default forwardRef(Main)
